﻿/*
	This file is part of Limpet.

    Limpet is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Limpet is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Limpet.  If not, see http://www.gnu.org/licenses/.
	
	Copyright 2010 Dan Popick
*/
using System;
using System.Collections.Generic;
using System.Configuration;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Web;

namespace Open.Web.Diagnostics.Limpet
{
    /// <summary>
    /// A manager class which controls access to the current module configuration
    /// </summary>
    public class LimpetManager
    {
        static public LimpetConfig Config { get; private set; }

        static LimpetManager()
        {
            Config = ConfigurationManager.GetSection("limpet") as LimpetConfig;
        }

        /// <summary>
        /// Gets a list of messages from the installed modules
        /// </summary>
        /// <returns>An enumerable set of strings</returns>
        static internal IEnumerable<SummaryMessage> GetMessages()
        {
            HttpContext context = HttpContext.Current;
            foreach (ILimpetModule module in Config.Modules)
            {
                if (module != null)
                {
                    SummaryMessage sm = null;

                    try
                    {
                        sm = module.GetMessage(context);
                    }
                    catch
                    {
                        // I should log here, but that would necessitate some kind of logging mechanism
                        // ...and if I log this condition... I should realy be logging about 50 others.
                        // And then where will the madness stop?

                        // return an error condition for the module, as these really should not fail.
                        sm = new SummaryMessage
                        {
                            Category = module.Name,
                            Severity = LimpetSeverity.Error,
                            Url = null,
                            Text = "[module generated an exception]"
                        };
                    }

                    yield return sm;
                }
            }
        }

        /// <summary>
        /// Gets the URL to request detail data from a self-rendering module
        /// </summary>
        /// <param name="name">Name of the module</param>
        /// <returns>A fully qualified URL to request detail data for that module</returns>
        public static string GetSelfRenderingUrl(string name)
        {
            HttpContext context = HttpContext.Current;

            string appPath = context.Request.ApplicationPath;
            return String.Format("{0}://{1}{2}{3}?module={4}",
                context.Request.Url.Scheme,
                context.Request.Url.Authority,
                appPath == "/" ? String.Empty : appPath,
                Config.BaseHandlerUrl,
                name);
        }

        /// <summary>
        /// Gets a module by name
        /// </summary>
        /// <param name="name">The name of the module</param>
        /// <returns>The module, or null if none is found</returns>
        static public ILimpetModule GetModule(string name)
        {
            foreach (ILimpetModule lm in Config.Modules)
            {
                if (lm.Name == name)
                    return lm;
            }

            return null;
        }
    }
}
